import org.gradle.api.DefaultTask
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

class ProvideBuildClasspathTask extends DefaultTask {
    @OutputFile File outFile

    @TaskAction
    void writeProperties() throws Exception {
        final Properties props = new Properties()

        String preinstrumentedKey = "robolectric.usePreinstrumentedJars"
        boolean usePreinstrumentedJars =
            Boolean.parseBoolean(
              System.getProperty(preinstrumentedKey, "true"))

        AndroidSdk.ALL_SDKS.each { androidSdk ->
            String coordinates =
              usePreinstrumentedJars ?
                androidSdk.preinstrumentedCoordinates : androidSdk.coordinates
            def config =
                project.configurations.create("sdk${androidSdk.apiLevel}")
            project.dependencies.add(
                "sdk${androidSdk.apiLevel}",
                coordinates)
            props.setProperty(
                coordinates,
                config.files.join(File.pathSeparator))
        }

        File outDir = outFile.parentFile
        if (!outDir.directory) outDir.mkdirs()
        outFile.withPrintWriter { out ->
            props.store(out, "# GENERATED by ${this} -- do not edit")
        }
    }
}
